﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Company.StudyClub.Model;
using Company.StudyClub.BLL.Site;

public partial class Access_Default : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRoleList();
        }
    }

    protected void BindRoleList()
    {
        RolesBLL objBLL = new RolesBLL();

        rpt_RoleList.DataSource = objBLL.GetRolesList(0);
        rpt_RoleList.DataBind();
        
        if (rpt_RoleList.Items.Count > 0)
        {
            btn_MultipleDelete.Enabled = false;
        }
    }

    protected void rpt_RoleList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            int roleId = Convert.ToInt32(e.CommandArgument);
            RolesBLL objBLL = new RolesBLL();
            objBLL.Delete(roleId);

            DeletRoleUser(roleId);
            DeletRoleFunction(roleId);

            BindRoleList();
        }
    }

    protected void btn_MultipleDelete_Click(object sender, EventArgs e)
    {
        for(int i = 0; i<rpt_RoleList.Items.Count;i++)
        {
            HtmlInputCheckBox cb_One = (HtmlInputCheckBox)rpt_RoleList.Items[i].FindControl("cb_One");
            if (cb_One.Checked)
            {
                int roleId = int.Parse(cb_One.Value);
                RolesBLL objBLL = new RolesBLL();

                objBLL.Delete(roleId);
                DeletRoleUser(roleId);
                DeletRoleFunction(roleId);
            }
        }
        BindRoleList();
    }

    protected void DeletRoleUser(int roleID)
    {
        RoleUsersBLL objBLL = new RoleUsersBLL();
        objBLL.Delete(roleID, 0);
    }

    protected void DeletRoleFunction(int roleID)
    {
        RoleFunctionsBLL objBLL = new RoleFunctionsBLL();
        objBLL.Delete(roleID, 0);
    }
}
